package org.example.register;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

//注册页面UI自动化测试
public class SignPageUI {

    public static WebDriver webDriver;

    @BeforeAll
    public static void before() {
        webDriver = new ChromeDriver();
        webDriver.get("http://123.60.88.108:8080/register.html");
    }

    //注册框检查
    @Order(1)
    @Test
    public void test1() {
        //测试登录框
        WebElement element1 = webDriver.findElement(By.cssSelector("body > div.login-container > div"));
        String e1 = element1.getAttribute("class");
        if(e1.equals("login-dialog")) {
            System.out.println("注册框架:测试成功");
        }else {
            System.out.println("注册框架：测试失败");
        }
    }

    //注册文字提示检查
    @Order(2)
    @Test
    public void test2() {
        WebElement element2 = webDriver.findElement(By.cssSelector("body > div.login-container > div > h3"));
        String text2 = element2.getText();
        if(text2.equals("注册")) {
            System.out.println("注册文字:测试成功");
        }else {
            System.out.println("登录文字：测试失败");
        }
    }

    //用户名文字检查
    @Order(3)
    @Test
    public void test3() {
        WebElement element3 = webDriver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(2) > span"));
        String text3 = element3.getText();
        if(text3.equals("用户名")){
            System.out.println("用户名文字:测试成功");
        }else {
            System.out.println("用户名文字测试失败");
        }
    }
    //密码文字检查
    @Order(4)
    @Test
    public void test4() {
        WebElement element = webDriver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(3) > span"));
        String text = element.getText();
        if(text.equals("密码")) {
            System.out.println("密码文字：测试成功");
        }else {
            System.out.println("密码文字测试失败");
        }
    }
    //密码输入框检查
    @Order(5)
    @Test
    public void test5() {
        WebElement element = webDriver.findElement(By.cssSelector("#password"));
        String attribute = element.getAttribute("id");
        if(attribute.equals("password")) {
            System.out.println("密码输入框：测试成功");
        }else {
            System.out.println("密码输入框测试失败");
        }
    }
    //用户名输入框检查
    @Order(6)
    @Test
    public void test6() {
        WebElement element = webDriver.findElement(By.cssSelector("#username"));
        String attribute = element.getAttribute("id");
        if(attribute.equals("username")) {
            System.out.println("用户名输入框：测试成功");
        }else {
            System.out.println("用户名输入框测试失败");
        }
    }


    //注册按钮检查
    @Order(7)
    @Test
    public void test7() {
        WebElement element = webDriver.findElement(By.cssSelector("#signIn"));
        String text = element.getText();
        if(text.equals("注册")) {
            System.out.println("注册按钮：测试成功");
        }else {
            System.out.println("注册按钮：测试失败");
        }
    }

    //弹窗检查
    @Order(8)
    @Test
    public void test8() {
        WebElement element1 = webDriver.findElement(By.cssSelector("#username"));
        WebElement element2 = webDriver.findElement(By.cssSelector("#password"));
        //输入错误账号和密码
        element1.sendKeys("zhangsan");
        element2.sendKeys("");
        WebElement button = webDriver.findElement(By.cssSelector("#signIn"));
        button.click();
        Alert alert = webDriver.switchTo().alert();
        if(alert.getText().equals("用户名或密码为空")) {
            System.out.println("弹框测试成功");
            alert.accept();
        }else {
            System.out.println("弹窗测试失败");
        }
    }

    @AfterAll
    public static void close() {
        webDriver.quit();
    }


}
